package com.example.add8;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.hyphenate.EMCallBack;
import com.hyphenate.chat.EMClient;

public class MainActivity extends AppCompatActivity {

    private EditText mEtName;
    private EditText mEtPsd;
    private Button mBtnLogin;
    private Button mBtnRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        boolean logged = EMClient.getInstance().isLoggedInBefore();
        if (logged){
            go2MainActivity();
        }
        setContentView(R.layout.activity_main);
        initPers();
        initViews();
    }
    private void initPers() {
        String[] pers = {
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.RECORD_AUDIO,
                Manifest.permission.CAMERA,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.READ_PHONE_STATE
        };
        ActivityCompat.requestPermissions(this, pers, 100);
    }
    private void initViews() {
        mEtName = findViewById(R.id.et_name);
        mEtPsd = findViewById(R.id.et_psd);
        mBtnLogin = findViewById(R.id.btn_login);
        mBtnRegister = findViewById(R.id.btn_register);
        mBtnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                login();
            }
        });
        mBtnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                go2Register();
            }
        });
    }
    private void go2Register() {
        Intent intent = new Intent(this,Main2Activity.class);
        startActivity(intent);
    }

    private void login() {
        //开发的时候一般不会自己直接new线程,一般会使用线程池
        final String name = mEtName.getText().toString().trim();
        final String psd = mEtPsd.getText().toString().trim();

        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(psd)) {
            Toast.makeText(this,"用户名和密码不能为空",Toast.LENGTH_LONG).show();
            return;
        }

        EMClient.getInstance().login(name,psd,new EMCallBack() {//回调
            @Override
            public void onSuccess() {
                EMClient.getInstance().groupManager().loadAllGroups();
                EMClient.getInstance().chatManager().loadAllConversations();
                Log.d("main", "登录聊天服务器成功！");
                showToast("登录成功");
                go2MainActivity();
            }

            @Override
            public void onProgress(int progress, String status) {

            }

            @Override
            public void onError(int code, String message) {
                Log.d("main", "登录聊天服务器失败！");
                showToast("登录失败");
            }
        });
    }

    private void go2MainActivity() {
        startActivity(new Intent(this, Main2Activity.class));
        finish();
    }

    public void showToast(final String msg){
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MainActivity.this,msg,Toast.LENGTH_LONG).show();
            }
        });
    }
}
